// 验证登录
const authCheck = (authStatus = false) => {
    // 获取本地存储
    let LoginAuth = uni.getStorageSync('LoginAuth') || '';

    if (!LoginAuth) {
        uni.showToast({
            title: '请先登录',
            icon: 'error',
            position: 'bottom',
            success: () => {
                if (authStatus === true) {
                    setTimeout(() => {
                        uni.redirectTo({
                            url: '/pages/login/login',
                        });
                    }, 1500);
                }
            },
        });

        return false;
    }

    return LoginAuth;
};

// 操作成功的轻提示
const ToastSuccess = (options) => {
    let title = '未知信息';
    let duration = 1500;
    let fn = () => {}

    if(typeof options !== 'undefined')
    {
        title = typeof options === 'string' ? options : options.title
    }

    if(typeof options === 'object')
    {
        duration = typeof options.duration === 'number' ? options.duration : 1500;
    }

    if(typeof options === 'object')
    {
        fn = typeof options.success === 'function' ? options.success : () => {};
    }

    uni.showToast({
        title: title,
        icon: 'success',
        position: 'bottom',
        duration: duration,
        success: fn,
    });
};

// 操作失败的轻提示
const ToastError = (options) => {
    let title = '未知信息';
    let duration = 1500;
    let fn = () => {}

    if(typeof options !== 'undefined')
    {
        title = typeof options === 'string' ? options : options.title
    }

    if(typeof options === 'object')
    {
        duration = typeof options.duration === 'number' ? options.duration : 1500;
    }

    if(typeof options === 'object')
    {
        fn = typeof options.success === 'function' ? options.success : () => {};
    }

    uni.showToast({
        title: title,
        icon: 'error',
        position: 'bottom',
        duration: duration,
        success: fn,
    });
};

const install = (Vue, vm) => {
    Vue.prototype.$Tools = {
        authCheck,
        ToastSuccess,
        ToastError,
    };
};

export default {
    authCheck,
    ToastSuccess,
    ToastError,
    install,
};
